<?php

/**
 * peticiones actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage peticiones
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class peticionesActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->personas = Doctrine_Core::getTable('persona')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->persona = Doctrine_Core::getTable('persona')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->persona);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new personaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new personaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($persona = Doctrine_Core::getTable('persona')->find(array($request->getParameter('id'))), sprintf('Object persona does not exist (%s).', $request->getParameter('id')));
        $this->form = new personaForm($persona);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($persona = Doctrine_Core::getTable('persona')->find(array($request->getParameter('id'))), sprintf('Object persona does not exist (%s).', $request->getParameter('id')));
        $this->form = new personaForm($persona);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($persona = Doctrine_Core::getTable('persona')->find(array($request->getParameter('id'))), sprintf('Object persona does not exist (%s).', $request->getParameter('id')));
        $persona->delete();

        $this->redirect('peticiones/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {

            $peticion = new Peticion();
            $persona = $form->save();
            $peticion->setDni($form->getObject()->getDni());
            $peticion->setFirstName($form->getObject()->getFirstName());
            $peticion->setLastName($form->getObject()->getLastName());
            $peticion->setSexo($form->getObject()->getSexo());
            $peticion->setEmailAddress($form->getObject()->getEmailAddress());
            $peticion->setDireccion($form->getObject()->getDireccion());
            $peticion->setTelefono($form->getObject()->getTelefono());
            $peticion->setTelefonoMovil($form->getObject()->getTelefonoMovil());
            $peticion->setUsername("");
            $peticion->setPassword("");
            $dir = explode("@", $form->getObject()->getEmailAddress());
            $email = $dir[1];
            if ($email == "gmail.com") {
                $q1 = Doctrine_Query::create()
                        ->from('sfguarduser u')
                        ->where('u.email_address=?', $form->getObject()->getEmailAddress());
                $obj = $q1->fetchOne();
                if (!$obj == NULL) {
                    $persona->delete();
                } else {
                    $peticion->save();
                    $persona->delete();

                    $this->getUser()->setFlash('notice', 'Su Petición ha sido enviada por favor revise su correo en 3 días para confirmar respuesta!');
                    $this->redirect('@sf_guard_signin');
                }
            } else {
                $persona->delete();
                $this->getUser()->setFlash('error', 'El email ingresado no tiene dominio de  GMAIL.COM');
            }







            //$this->redirect('peticiones/edit?id='.$persona->getId());
        }
    }

}
